<?php

class ClientLogSender {
	public function send($str)	{
		$opts = array
		(
			'http' => array
			(
			'method' => 'POST',
			'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
			"Content-Length: " . strlen($str) . "\r\n",
			'content' => $str,
			),
		);
		$context = stream_context_create($opts);
		$respone = file_get_contents("http://rt5.mo.baidu.mobi:10000/phone/uplog.gbza?", false, $context);
		return $respone;
	}
	
	public function genAudioData($url,$arrSeq)	{
		$strAudioData = "";
		foreach ( $arrSeq  as $value) {
			$tmp = pack("C1V1",$value['t'],$value['v']);
			$strAudioData .= $tmp;
		}
		$urlLen = strlen($url);
		$urlHeader = pack("v1",$urlLen);
		$strAudioData = $urlHeader.$url.$strAudioData;
		
		$audioDataLen  = strlen($strAudioData);
		$audioBlockLen = $audioDataLen + 4;
		$audioBlockHeader = pack("C2v1",0x04,0x00,$audioBlockLen);
		$audioBlockData = $audioBlockHeader . $strAudioData;
		$audioBlockData = gzencode($audioBlockData);
		
		$uploadLen = strlen($audioBlockData) + 12;
		$uploadHeader = pack("V3",28,$uploadLen,0);
		return $uploadHeader.$audioBlockData;
	}
}

$sender = new ClientLogSender();

$str = "";
$url = "test.gbza?";
$arrSeq = array(
	array('t'=>0, 'v'=>0),
	array('t'=>1, 'v'=>500),
	array('t'=>2, 'v'=>100),
	array('t'=>3, 'v'=>50),
	array('t'=>2, 'v'=>101),
	array('t'=>3, 'v'=>51),
	array('t'=>4, 'v'=>1000),
	array('t'=>5, 'v'=>800),
);

$str = $sender->genAudioData($url,$arrSeq);
$respone = $sender->send($str);
echo $respone;